//
//  FSNewMessageController.m
//  WZSOA
//
//  Created by 刘沿 on 16/7/6.
//  Copyright © 2016年 刘沿. All rights reserved.
//

#import "FSNewMessageController.h"

@interface FSNewMessageController ()

@end

@implementation FSNewMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"新建";
    UIBarButtonItem * rightItem  = rightItem  = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(sendSMS)];;
    self.navigationItem.rightBarButtonItem = rightItem;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSelectName:) name:@"PostSelectNameNoti" object:nil];
    [self setupTextFieldRightView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectNO:) name:@"111" object:nil];
    
}

- (void)selectNO:(NSNotification *)no{
    
    NSMutableArray *array = no.userInfo[@"text"];
    
    NSString *str = nil;
    
    for (NSString *str1 in array) {
        
        if (str.length == 0) {
            
            str = [NSString stringWithFormat:@"%@",str1];
        }else {
            
            str = [NSString stringWithFormat:@"%@,%@",str1,str];
        }
        
    }
    
    self.receiveTextField.text = str;
    
}
-(void)getSelectName:(NSNotification*)note{
    
    NSArray * array = note.userInfo[@"SelectName"];
    self.receiveTextField.text = [array componentsJoinedByString:@","];
    
}


-(void)sendSMS{
    
    if (self.contentsTextView.text.length == 0) {
        [self.view.window makeToast:@"输入短信内容"];
        return;
    }
    
    if (self.receiveTextField.text.length == 0 && self.phoneNumber.text.length == 0 ) {
        [self.view.window makeToast:@"请选择收件人或填写手机号"];
        return;
    }
    
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    //    if (self.contentsTextView.text.length !=0) {
    //        [dict setObject:self.contentsTextView.text forKey:@"Contents"];
    //    }
    [dict setObject:self.contentsTextView.text forKey:@"Contents"];
    
    //    if (self.receiveTextField.text.length !=0) {
    //        [dict setObject:self.receiveTextField.text forKey:@"Receives"];
    //    }
    [dict setObject:self.receiveTextField.text forKey:@"Receives"];
    
    //    if (self.phoneNumber.text.length !=0) {
    //        [dict setObject:self.phoneNumber.text forKey:@"PhoneNumber"];
    //    }
    [dict setObject:self.phoneNumber.text forKey:@"PhoneNumber"];
    
    NSData * data  = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
    NSString * infoString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array =@[@{@"userId":userId},@{@"formxml":infoString}];
    [HttpTools requestWithURL:Kexchother arrays:array method:KMessageSend success:^(id JSON) {
        [self.view.window makeToast:@"提交短信成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [self.view.window makeToast:@"提交短信失败"];
    }];
    
    
    
}

#pragma mark 设置右边的view
-(void)setupTextFieldRightView{
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    rightView.backgroundColor = [UIColor clearColor];
    UIButton * addNameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addNameBtn setImage:[UIImage imageNamed:@"addName.png"] forState:UIControlStateNormal];
    
    [addNameBtn addTarget:self action:@selector(addNameAction) forControlEvents:UIControlEventTouchUpInside];
    
    addNameBtn.frame = CGRectMake(0, 0, 40, 40);
    [rightView addSubview:addNameBtn];
    
    self.receiveTextField.rightView = addNameBtn;
    self.receiveTextField.rightViewMode=UITextFieldViewModeAlways;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 1) {
        [self addNameAction];
    }
}

-(void)addNameAction{
    [self keybordDown];
//    UIStoryboard * stroyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    OAMainAddressViewController * mainAddressVC = [stroyBoard instantiateViewControllerWithIdentifier:@"mainAddressVCID"];
//    mainAddressVC.addressType = LessMainAddressType;
//    [self.navigationController pushViewController:mainAddressVC animated:YES];

    OANameListController * listVC = (OANameListController*)[self.storyboard instantiateViewControllerWithIdentifier:@"NameLIstID"];
    
    [self.navigationController pushViewController:listVC animated:YES];

}

- (void)keybordDown {
    [self.contentsTextView resignFirstResponder];
    [self.receiveTextField resignFirstResponder];
    [self.phoneNumber resignFirstResponder];
}



@end



















